#include <libpub.h>

extern "C"{
int AudioInit(void);
int Codec_SetVolume(unsigned char left, unsigned char right, unsigned int volType);
int PlaySound();
void Audio_Close(void);
}

void AudioClose();

const unsigned short c_speakerVolumeTable[] = {
    //0,  1,  2,  3,  4,  5,  6,  7, 8, 9, 10, 11, 12, 13, 14, 15     input
     31, 18, 15, 12, 10,  8,  7,  6, 5, 4,  3,  3,  2,  2,  1,  0  // ouput
    };

int AudioTest(int vol)
{
    TOUT(1, 1, printf("\nAudioTest()"));
    AudioInit();
#define VOLUMETYPE_SPEAKER  0
    Codec_SetVolume(vol, vol, VOLUMETYPE_SPEAKER);
    PlaySound();
    AudioClose();

    return 0;
}

void AudioClose()
{
    Audio_Close();
}

//==================
int _Do_Test_Audio(char *param)
{
     TOUT(1, 1, printf("\n_Do_Test_Audio(%s):\n", param));
     char *func = NULL;
     if(*param){//test audio
        param = Bo_ParseString(param, &func);
        if(0 == Bo_MatchString(func , "play")) {//test audio play
            //Do test audio play code
            printf("\ntest audio play...\n");
            AudioTest(c_speakerVolumeTable[14]);//default volume level is highest
        }
        else if(0 <= Bo_MatchString("volume", func)){//test audio volume
            if(*param) {
                param = Bo_ParseString(param, &func);
                if(0 == Bo_MatchString(func, "auto")){//test audio volume auto
                    printf("\nvolume auto\n");
                    for(int i = 0; i <= 15; i++){
                        printf("\nPlay at volume level %2d ...", i);
                        AudioTest(c_speakerVolumeTable[i]);//TODO: Choice new audio file
                    }
                }
                else {//test audio volume {val}
                    int volume = Bo_ConvertString(func);
                    printf("\nPlay at volume = %d ...", volume);
                    if (volume <= 0 || volume > 31) {
                            printf("Invalid volume value\n");
                            return -1;
                    }
                    else {
                        AudioTest(volume);//TODO: Choice new audio file
                    }
                }
            }
        }
     }

    AudioClose();
    return 0;
}
